﻿#include <mzfc_inc.h>
#include "HexagonMineConfigWnd.h"

MZ_IMPLEMENT_DYNAMIC(CHexagonMineConfigWnd)

CHexagonMineConfigWnd::CHexagonMineConfigWnd(hexagon_mine::CHexagonMineGame* game)
: m_Game(game)
{
}

CHexagonMineConfigWnd::~CHexagonMineConfigWnd(void)
{
}

// 窗口的初始化
BOOL CHexagonMineConfigWnd::OnInitDialog()
{
	// 必须先调用基类的初始化
    if (!CMzWndEx::OnInitDialog())
    {
		return FALSE;
    }

	m_Game->GetConfig(m_Config);

	int next_y = 0;

	m_Caption.SetPos(0, 0, GetWidth(), MZM_HEIGHT_CAPTION);
	m_Caption.SetText(L"蜂窝扫雷设置");
	AddUiWin(&m_Caption);

	next_y += MZM_HEIGHT_CAPTION;

	m_About.SetPos(0, next_y, GetWidth(), MZM_HEIGHT_SINGLELINE_EDIT);
	m_About.SetText(L"Copyright(c) 2009-2010 BenBear");
	m_About.SetEditStyle(SINGLELINE_EDIT_STYLE_HCENTER);
	m_About.EnableGrabFocusByMouse(false);
	AddUiWin(&m_About);

	next_y += MZM_HEIGHT_SINGLELINE_EDIT;

	m_MinePercentTitle.SetText(L"地雷百分比");
	m_MinePercent.SetRange(hexagon_mine::MIN_PERCENT, hexagon_mine::MAX_PERCENT);
	m_MinePercent.SetPadpos(m_Config.MinePercent);
	m_MinePercent.SetID(MZ_IDC_HMC_MINE_PERCENT);
	UpdateStatusBar(m_MinePercent, m_MinePercentNumber);
	AddStatusBar(next_y, m_MinePercentTitle, m_MinePercent, m_MinePercentNumber);

	m_WidthTitle.SetText(L"雷区宽度");
	m_Width.SetRange(3, hexagon_mine::MAX_WIDTH);
	m_Width.SetPadpos(m_Config.Width);
	m_Width.SetID(MZ_IDC_HMC_WIDTH);
	m_IsOriginalWidth = true;
	UpdateStatusBar(m_Width, m_WidthNumber);
	AddStatusBar(next_y, m_WidthTitle, m_Width, m_WidthNumber);

	m_HeightTitle.SetText(L"雷区高度");
	m_Height.SetRange(3, hexagon_mine::MAX_HEIGHT);
	m_Height.SetPadpos(m_Config.Height);
	m_Height.SetID(MZ_IDC_HMC_HEIGHT);
	UpdateStatusBar(m_Height, m_HeightNumber);
	AddStatusBar(next_y, m_HeightTitle, m_Height, m_HeightNumber);

	m_RandomSizeTitle.SetText(L"随机雷区宽、高");
	m_RandomSize.SetID(MZ_IDC_HMC_RANDOM_SIZE);
	AddSwitch(next_y, m_RandomSizeTitle, m_RandomSize, m_Config.RandomSize);

	m_RightSideTitle.SetText(L"扩展右侧使每行数目相同");
	m_RightSide.SetID(MZ_IDC_HMC_RIGHT_SIDE);
	AddSwitch(next_y, m_RightSideTitle, m_RightSide, m_Config.HasRightSideMine);

	m_HexagonShapeTitle.SetText(L"正六边形雷区");
	m_HexagonShape.SetID(MZ_IDC_HMC_HEXAGON_SHAPE);
	AddSwitch(next_y, m_HexagonShapeTitle, m_HexagonShape, m_Config.HexagonShape);

	m_AutoLockMineTitle.SetText(L"自动锁定雷");
	m_AutoLockMine.SetID(MZ_IDC_HMC_AUTO_LOCK_MINE);
	AddSwitch(next_y, m_AutoLockMineTitle, m_AutoLockMine, m_Config.AutoLockMine);

	m_MineDetectorTitle.SetText(L"使用自动地雷探测针");
	m_MineDetector.SetID(MZ_IDC_HMC_MINE_DETECTOR);
	AddSwitch(next_y, m_MineDetectorTitle, m_MineDetector, m_Config.UseDetector);


	m_Toolbar.SetPos(0, GetHeight() - MZM_HEIGHT_TEXT_TOOLBAR, GetWidth(), MZM_HEIGHT_TEXT_TOOLBAR);
	m_Toolbar.SetButton(0, true, true, L"取消");
	m_Toolbar.SetButton(2, true, true, L"确定");
	m_Toolbar.SetID(MZ_IDC_HMC_TOOLBAR);
	AddUiWin(&m_Toolbar);

	UpdateRandomSize();
	UpdateHexagonShape();

	return TRUE;
}

// 重载 MZFC 的命令消息处理函数
void CHexagonMineConfigWnd::OnMzCommand(WPARAM wParam, LPARAM lParam)
{
	UINT_PTR id = LOWORD(wParam);
	switch(id)
	{
	case MZ_IDC_HMC_TOOLBAR:
		{
			int nIndex = lParam;
			if (nIndex == 0)
			{
				EndModal(ID_CANCEL);
				return;
			}
			else if (nIndex == 2)
			{
				SaveConfig();

				EndModal(ID_OK);
				return;
			}
		}
		break;
	case MZ_IDC_HMC_AUTO_LOCK_MINE:
		{
			UpdateAutoLockMine();
		}
		break;
	case MZ_IDC_HMC_RANDOM_SIZE:
		{
			UpdateRandomSize();
		}
		break;
	case MZ_IDC_HMC_HEXAGON_SHAPE:
		{
			UpdateHexagonShape();
		}
		break;
	case MZ_IDC_HMC_MINE_DETECTOR:
		{
			UpdateMineDetector();
		}
		break;
	case MZ_IDC_HMC_RIGHT_SIDE:
		{
			UpdateRightSide();
		}
		break;
	default:
		break;
	}
}

LRESULT CHexagonMineConfigWnd::MzDefWndProc(UINT message, WPARAM wParam, LPARAM lParam)
{
	switch (message)
	{
	case MZ_MOUSE_DOWN_MOVE:
		{
			if (wParam == MZ_IDC_HMC_WIDTH)
			{
				UpdateStatusBar(m_Width, m_WidthNumber);
			}
			else if (wParam == MZ_IDC_HMC_HEIGHT)
			{
				UpdateStatusBar(m_Height, m_HeightNumber);
			}
			else if (wParam == MZ_IDC_HMC_MINE_PERCENT)
			{
				UpdateStatusBar(m_MinePercent, m_MinePercentNumber);
			}
		}
		break;
	}

	return CMzWndEx::MzDefWndProc(message,wParam,lParam);
}


void CHexagonMineConfigWnd::AddSwitch(int& y, UiStatic &title, UiButtonEx &button, bool on)
{
	const int WIDTH_SWITCH = 120;
	const int HEIGHT = MZM_HEIGHT_BUTTONEX;

	title.SetPos(0, y, GetWidth() - WIDTH_SWITCH, HEIGHT);
	title.SetDrawTextFormat(DT_LEFT | DT_VCENTER);
	AddUiWin(&title);

	button.SetPos(GetWidth() - WIDTH_SWITCH, y, WIDTH_SWITCH, HEIGHT);
	button.SetButtonType(MZC_BUTTON_SWITCH);
	button.SetButtonMode(MZC_BUTTON_MODE_HOLD);
	if (on)
		button.SetState(MZCS_BUTTON_PRESSED);
	AddUiWin(&button);

	y += HEIGHT;
}

void CHexagonMineConfigWnd::AddStatusBar(int& y, UiStatic &title, UiStatusBar &status, UiStatic &number)
{
	const int WIDTH_STATUS = 240;
	const int WIDTH_NUMBER = 80;
	const int HEIGHT = MZM_HEIGHT_CAPTION;

	title.SetPos(0, y, GetWidth() - WIDTH_NUMBER - WIDTH_STATUS, HEIGHT);
	title.SetDrawTextFormat(DT_LEFT | DT_VCENTER);
	AddUiWin(&title);

	status.SetPos(GetWidth() - WIDTH_NUMBER - WIDTH_STATUS, y, WIDTH_STATUS, HEIGHT);
	AddUiWin(&status);

	number.SetPos(GetWidth() - WIDTH_NUMBER, y, WIDTH_NUMBER, HEIGHT);
	AddUiWin(&number);

	y += HEIGHT;
}

void CHexagonMineConfigWnd::UpdateStatusBar(UiStatusBar& status, UiStatic& number)
{
	int pos = status.GetPadpos();

	if (!m_IsOriginalWidth && (status.GetID() == MZ_IDC_HMC_WIDTH))
	{
		pos = (pos - 3) * 2 + 3;
	}

	wchar_t buf[32];
	swprintf(buf, L"%d", pos);
	number.SetText(buf);
	number.Invalidate();
}

void CHexagonMineConfigWnd::UpdateAutoLockMine()
{
	// nothing todo
}

void CHexagonMineConfigWnd::UpdateRandomSize()
{
	bool random_size = m_RandomSize.GetState() == MZCS_BUTTON_PRESSED;
	bool hexagon_shape = m_HexagonShape.GetState() == MZCS_BUTTON_PRESSED;
	if (random_size)
	{
		m_Width.SetEnable(false);
		m_Height.SetEnable(false);
	}
	else
	{
		m_Width.SetEnable(true);
		if (!hexagon_shape)
			m_Height.SetEnable(true);
	}
	m_Width.Invalidate();
	m_Height.Invalidate();
}

void CHexagonMineConfigWnd::UpdateHexagonShape()
{
	bool hexagon_shape = m_HexagonShape.GetState() == MZCS_BUTTON_PRESSED;
	bool random_size = m_RandomSize.GetState() == MZCS_BUTTON_PRESSED;
	if (hexagon_shape)
	{
		if (m_IsOriginalWidth)
		{
			m_IsOriginalWidth = false;
			int w = (m_Width.GetPadpos() - 3) / 2 + 3;
			m_Width.SetRange(3, (hexagon_mine::MAX_WIDTH - 3) / 2 + 3);
			m_Width.SetPadpos(w);
			m_Width.Invalidate();
			UpdateStatusBar(m_Width, m_WidthNumber);
		}

		if (!random_size)
		{
			m_Height.SetEnable(false);
			m_Height.Invalidate();
		}

		m_RightSide.SetState(MZCS_BUTTON_NORMAL);
		m_RightSide.SetEnable(false);
	}
	else
	{
		if (!m_IsOriginalWidth)
		{
			m_IsOriginalWidth = true;
			int w = (m_Width.GetPadpos() - 3) * 2 + 3;
			m_Width.SetRange(3, hexagon_mine::MAX_WIDTH);
			m_Width.SetPadpos(w);
			m_Width.Invalidate();
			UpdateStatusBar(m_Width, m_WidthNumber);
		}

		if (!random_size)
		{
			m_Height.SetEnable(true);
			m_Height.Invalidate();
		}

		m_RightSide.SetEnable(true);
	}
	m_RightSide.Invalidate();
}

void CHexagonMineConfigWnd::UpdateMineDetector()
{
	// nothing todo
}

void CHexagonMineConfigWnd::UpdateRightSide()
{
	// nothing todo
}

void CHexagonMineConfigWnd::SaveConfig()
{
	m_Config.AutoLockMine = m_AutoLockMine.GetState() == MZCS_BUTTON_PRESSED;
	m_Config.HasRightSideMine = m_RightSide.GetState() == MZCS_BUTTON_PRESSED;
	m_Config.HexagonShape = m_HexagonShape.GetState() == MZCS_BUTTON_PRESSED;
	m_Config.RandomSize = m_RandomSize.GetState() == MZCS_BUTTON_PRESSED;
	m_Config.UseDetector = m_MineDetector.GetState() == MZCS_BUTTON_PRESSED;
	
	int width = m_Width.GetPadpos();
	if (m_Config.HexagonShape)
		width = (width - 3) * 2 + 3;
	m_Config.Width = width;
	m_Config.Height = m_Height.GetPadpos();
	m_Config.MinePercent = m_MinePercent.GetPadpos();
}
